home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / gadlay17.lha / Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  6.8 KB  |  285 lines

  1.  
  2. /**
  3.  **  Test.c -- An example of using the GadLayout dynamic gadget layout system.
  4.  **
  5.  **  By Timothy Aston, public domain.
  6.  **
  7.  **    A "do nothing" example that simpy creates a bunch of gadgets in a
  8.  **    window and waits for the user to close the window.  The purpose is
  9.  **    to demonstrate many of the features available in GadLayout for laying
  10.  **    out gadgets.
  11.  **
  12.  **    I wanted to write a slightly more extensive demonstration of what
  13.  **    GadLayout can do, this doesn't show a lot of what can be done.
  14.  **
  15.  **    Compiled using DICE 2.07.54: dcc Test.c gadlayout.o -o Test -r
  16.  **
  17.  **/
  18.  
  19. #include <exec/types.h>
  20. #include <intuition/intuition.h>
  21. #include <libraries/gadtools.h>
  22. #include <libraries/locale.h>
  23. #include <utility/tagitem.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/gadtools_protos.h>
  26. #include <clib/locale_protos.h>
  27. #include "gadlayout.h"
  28. #include "gadlayout_protos.h"
  29.  
  30. #ifndef GTCB_Scaled
  31. #define GTCB_Scaled GT_TagBase + 68
  32. #endif
  33.  
  34. #define LEFT_OFFSET 6
  35. #define TOP_OFFSET 4
  36.  
  37. struct Library *LocaleBase = NULL;    /* LocaleBase MUST be defined, even if
  38.                                      * you don't use it!!! */
  39. struct Screen *screen = NULL;
  40. struct Window *win = NULL;
  41. APTR gi = NULL;
  42.  
  43. UBYTE test_str[100];                /* Buffer for the string gadget */
  44.  
  45.  
  46. /* Image data for an image button we will be defining.
  47.  */
  48. __chip UWORD image_data[64] =
  49. {
  50.     /* Plane 0 */
  51.     0x0000,0x0000,
  52.     0x0000,0x0000,
  53.     0x001E,0x0000,
  54.     0x0021,0x0000,
  55.     0x0040,0x8000,
  56.     0x0040,0x8000,
  57.     0x0040,0x8000,
  58.     0x0021,0x0000,
  59.     0x007E,0x0000,
  60.     0x00E0,0x0000,
  61.     0x01C0,0x0000,
  62.     0x0380,0x0000,
  63.     0x0700,0x0000,
  64.     0x0600,0x0000,
  65.     0x0000,0x0000,
  66.     0x0000,0x0000,
  67.     /* Plane 1 */
  68.     0x0000,0x0000,
  69.     0x0000,0x0000,
  70.     0x0000,0x0000,
  71.     0x0000,0x0000,
  72.     0x0018,0x0000,
  73.     0x0010,0x0000,
  74.     0x0000,0x0000,
  75.     0x0000,0x0000,
  76.     0x0000,0x0000,
  77.     0x0000,0x0000,
  78.     0x0000,0x0000,
  79.     0x0000,0x0000,
  80.     0x0000,0x0000,
  81.     0x0000,0x0000,
  82.     0x0000,0x0000,
  83.     0x0000,0x0000
  84. };
  85.  
  86. struct Image image =
  87. {
  88.     0, 0, 22, 16, 2, &image_data[0], 0xff, 0x0, NULL
  89. };
  90.  
  91.  
  92. /* Here are the tag lists that define each gadget.  Note that a gadget
  93.  * consists of a GadLayout tag list as well as a GadTools tag list (except
  94.  * for the GadLayout extended gadget kinds).
  95.  */
  96.  
  97. enum { GAD_BUTTON1, GAD_BUTTON2, GAD_BUTTON3, GAD_DRAWER, GAD_STRING,
  98.         GAD_CHECKBOX };
  99.  
  100. struct TagItem button1_layout_tags[] =
  101. {
  102.     { GL_GadgetKind, BUTTON_KIND },
  103.     { GL_GadgetText, "Button _1" },
  104.     { GL_Left, LEFT_OFFSET },
  105.     { GL_Top, TOP_OFFSET },
  106.     { GL_AutoHeight, 4 },
  107.     { GL_DupeWidth, GAD_BUTTON2 },
  108.     { GL_Flags, PLACETEXT_IN },
  109.     { TAG_DONE, NULL }
  110. };
  111.  
  112. struct TagItem button1_gadtools_tags[] =
  113. {
  114.     { GT_Underscore, '_' },
  115.     { TAG_DONE, NULL }
  116. };
  117.  
  118. struct TagItem button2_layout_tags[] =
  119. {
  120.     { GL_GadgetKind, BUTTON_KIND },
  121.     { GL_GadgetText, "And Button _2" },
  122.     { GL_AutoWidth, 10 },
  123.     { GL_AutoHeight, 4 },
  124.     { GL_TopRel, GAD_BUTTON1 },
  125.     { GL_AddTop, INTERHEIGHT },
  126.     { TAG_DONE, NULL }
  127. };
  128.  
  129. struct TagItem button2_gadtools_tags[] =
  130. {
  131.     { GT_Underscore, '_' },
  132.     { TAG_DONE, NULL }
  133. };
  134.  
  135. struct TagItem button3_layout_tags[] =
  136. {
  137.     { GL_GadgetKind, IMAGEBUTTON_KIND },
  138.     { GL_GadgetText, "Button _3" },
  139.     { GL_Width, 26 },
  140.     { GL_Height, 18 },
  141.     { GL_TopRel, GAD_BUTTON2 },
  142.     { GL_AddTop, INTERHEIGHT },
  143.     { GL_Flags, PLACETEXT_RIGHT },
  144.     { GLIM_Image, &image },
  145.     { TAG_DONE, NULL }
  146. };
  147.  
  148. struct TagItem drawer_layout_tags[] =
  149. {
  150.     { GL_GadgetKind, DRAWER_KIND },
  151.     { GL_GadgetText, "_Filename" },
  152.     { GL_Top, TOP_OFFSET },
  153.     { GL_LeftRel, GAD_BUTTON1 },
  154.     { GL_AdjustLeft, INTERWIDTH * 2},
  155.     { GL_Width, 20 },
  156.     { GL_AutoHeight, 4 },
  157.     { GL_Flags, PLACETEXT_LEFT },
  158.     { TAG_DONE, NULL }
  159. };
  160.  
  161. struct TagItem string_layout_tags[] =
  162. {
  163.     { GL_GadgetKind, STRING_KIND },
  164.     { GL_GadgetText, NULL },
  165.     { GL_Width, 100 },
  166.     { GL_LeftRel, GAD_DRAWER },
  167.     { TAG_DONE, NULL }
  168. };
  169.  
  170. struct TagItem string_gadtools_tags[] =
  171. {
  172.     { GTST_MaxChars, 100 },
  173.     { GTST_String, test_str },
  174.     { GT_Underscore, '_' },
  175.     { TAG_DONE, NULL }
  176. };
  177.  
  178. struct TagItem checkbox_layout_tags[] =
  179. {
  180.     { GL_GadgetKind, CHECKBOX_KIND },
  181.     { GL_GadgetText, "Checkbox" },
  182.     { GL_Width, 26 },
  183.     { GL_AutoHeight, 1 },
  184.     { GL_TopRel, GAD_DRAWER },
  185.     { GL_AddTop, INTERHEIGHT },
  186.     { GL_AlignRight, GAD_STRING },
  187.     { GL_Flags, PLACETEXT_LEFT },
  188.     { TAG_DONE, NULL }
  189. };
  190.  
  191. struct TagItem checkbox_gadtools_tags[] =
  192. {
  193.     { GTCB_Checked, TRUE },
  194.     { GTCB_Scaled, TRUE },
  195.     { GT_Underscore, '_' },
  196.     { TAG_DONE, NULL }
  197. };
  198.  
  199.  
  200. /* This structure describes all the gadgets we have just defined, so that
  201.  * they can all be passed at once to the LayoutGadgets() function.
  202.  */
  203. struct LayoutGadget gadgets[] =
  204. {
  205.     { GAD_BUTTON1,    button1_layout_tags,    button1_gadtools_tags,    NULL },
  206.     { GAD_BUTTON2,    button2_layout_tags,    button2_gadtools_tags,    NULL },
  207.     { GAD_BUTTON3,    button3_layout_tags,    NULL,                    NULL },
  208.     { GAD_DRAWER,    drawer_layout_tags,        NULL,                    NULL },
  209.     { GAD_STRING,    string_layout_tags,        string_gadtools_tags,    NULL },
  210.     { GAD_CHECKBOX,    checkbox_layout_tags,    checkbox_gadtools_tags,    NULL },
  211.     { -1, NULL, NULL, NULL }
  212. };
  213.  
  214.  
  215. main()
  216. {
  217.     struct Gadget *glist;
  218.     WORD farright, farbottom;
  219.  
  220.     /* We'll just open up on the Workbench screen, and use its screen font.
  221.      * Try changing the screen font in your Font prefs to all sorts of
  222.      * ridiculous sizes and see how well this simple example adjust.
  223.      */
  224.     if (screen = LockPubScreen("Workbench"))
  225.     {
  226.         if (gi = LayoutGadgets(&glist, gadgets, screen,
  227.             GL_RightExtreme, &farright,
  228.             GL_LowerExtreme, &farbottom,
  229.             GL_DefTextAttr, screen->Font,
  230.             TAG_DONE))
  231.         {
  232.             /* Open the window, note how we size the window to perfectly fit
  233.              * all the gadgets.
  234.              */
  235.             if (win = OpenWindowTags(NULL,
  236.                 WA_Left, 0,
  237.                 WA_Top, screen->Font->ta_YSize + 3,
  238.                 WA_InnerWidth, farright + LEFT_OFFSET,
  239.                 WA_InnerHeight, farbottom + TOP_OFFSET,
  240.                 WA_IDCMP, BUTTONIDCMP | STRINGIDCMP | IDCMP_REFRESHWINDOW |
  241.                             IDCMP_CLOSEWINDOW,
  242.                 WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  243.                             WFLG_ACTIVATE | WFLG_SMART_REFRESH |
  244.                             WFLG_GIMMEZEROZERO,
  245.                 WA_Gadgets, glist,
  246.                 WA_Title, "GadLayout Test",
  247.                 TAG_DONE))
  248.             {
  249.                 struct IntuiMessage *imsg;
  250.                 BOOL ok = TRUE;
  251.  
  252.                 /* printf("Button 1 key command: %c\n", GadgetKeyCmd(gi, GAD_BUTTON1, gadgets));
  253.                 printf("Button 2 key command: %c\n", GadgetKeyCmd(gi, GAD_BUTTON2, gadgets));
  254.                 printf("Drawer key command: %c\n", GadgetKeyCmd(gi, GAD_DRAWER, gadgets));
  255.                 printf("String key command: %c\n", GadgetKeyCmd(gi, GAD_STRING, gadgets));
  256.                 printf("Checkbox key command: %c\n", GadgetKeyCmd(gi, GAD_CHECKBOX, gadgets)); */
  257.  
  258.                 /* Just wait around until the close gadget is pressed.
  259.                  */
  260.                 while (ok)
  261.                 {
  262.                     WaitPort(win->UserPort);
  263.                     while (imsg = GT_GetIMsg(win->UserPort))
  264.                     {
  265.                         if (imsg->Class == IDCMP_CLOSEWINDOW)
  266.                             ok = FALSE;
  267.                         GT_ReplyIMsg(imsg);
  268.                     }
  269.                 }
  270.                 CloseWindow(win);
  271.             }
  272.             else
  273.                 PutStr("ERROR: Couldn't open window\n");
  274.  
  275.             FreeLayoutGadgets(gi);
  276.         }
  277.         else
  278.             PutStr("ERROR: Couldn't layout gadgets\n");
  279.  
  280.         UnlockPubScreen(0, screen);
  281.     }
  282.     else
  283.         PutStr("ERROR: Couldn't lock public screen\n");
  284. }
  285.